package cn.tedu.boke.portal.security;

import cn.tedu.boke.portal.service.impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author Xdoor
 * @version 1.0
 * @date 2021/10/28 11:36
 */

@Configuration
// 启动Spring-Security提供的权限管理功能
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    //当前类继承WebSecurityConfigurerAdapter
    //能够重写这个父类中的方法,这个父类中的方法都是用于设置权限管理

    @Autowired
    protected UserDetailsServiceImpl userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.userDetailsService(userDetailsService);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.csrf().disable()//禁用防跨域攻击功能
                .authorizeRequests()
                .antMatchers(
                        "/css/*",
                        "/js/*",
                        "/reg_login.html",
                        "/reg",
                        "/login",
                        "/index.html",
                        "/boke/post/labels",
                        "/one_post.html",
                        "/boke/post/labels/one/*")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/reg_login.html")
                .loginProcessingUrl("/login")
                .failureUrl("/reg_login.html?login_error")
                .defaultSuccessUrl("/index.html")
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login.html")
                .and()
                .headers().frameOptions().disable();
    }
}
